import { parse } from "querystring";

const reg =
  /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;
// import { parse } from "querystring";

// const reg =
//   /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;

// export const isUrl = (path: string): boolean => reg.test(path);

export const getPageQuery = () => parse(window.location.href.split("?")[1]);
// export const getPageQuery = () => parse(window.location.href.split("?")[1]);

export const APP_ROUTE = "/cnpc-route";
export const PATH_ROUTE = "/cnpc-web";
export const DEFAULT_EMPTY_STR = "--";

// 判断是否是调试环境
export const isDEV = process.env.envType === "dev";

export const enum PLATFORM_TYPE {
  "dev" = "dev",
  "obp" = "obp",
  "supOS" = "supOS",
}

export const getWsPreUrl = (url = "/cnpc-web/websocket/") => {
  if (isDEV) {
    return `ws://10.54.48.42:31527/cnpc-web/websocket`;
  }
  // 发布
  const protocol: string = window.location.protocol;
  if (protocol === "http:") {
    return `ws://${window.origin.replace("http://", "")}/${url}`;
  }
  return `wss://${window.origin.replace("https://", "")}/${url}`;
};

export function isEndingWithPunctuation(text: string) {
  if (text.length === 0) {
    return false;
  }
  const lastChar = text[text.length - 1];
  const punctuation = /[.,!?;:'"(){}[\]<>]/;
  return punctuation.test(lastChar);
}

// 保留小数位显示
export const adpterShowNumber = (v, n=2)=>{
  let num = parseFloat(v);
  let cValue = !isNaN(num) ? num.toFixed(n) : '--';
  return cValue
}